<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/11
 * Time: 22:50
 */

namespace F4soft\glass;


use F4soft\library\Database;

class SyncMasterData {
	private static $instance;


	private static $apiUrl = "http://10.2.98.197/api/mainData/getAllInfo";

	// 每次拉取多久之前的数据
	const  LAST_UPDATE_INTERVAL = 24 * 60 * 60 * 1000;
	// 主数据用户在用状态key值
	const  USER_STATUS_NORMAL = "f59ef40d948240a291e378d365fef703";

	/**
	 * @return \F4soft\glass\SyncMasterData
	 */
	public static function getInstance() {
		if ( self::$instance == null ) {
			self::$instance = new self();
		}

		return self::$instance;
	}


	private function beforeAction() {
		$sql = file_get_contents( __DIR__ . "/../../sql/sync_data.sql" );
		Database::getInstance()->query( $sql );
	}

	public function main() {
		$this->beforeAction();
		$this->insertEmp();
		$this->insertOrg();
	}

	public function insertOrg() {
		Database::getInstance()->query( "truncate table gangwan_org" );
		foreach ( $this->getAllOrg() as $key => $value ) {
			echo "------insertOrg---------------" . json_encode($value);
			Database::getInstance()->insert( "gangwan_org", $value );
		}
	}


	public function insertEmp() {
		Database::getInstance()->query( "truncate table gangwan_emp" );
		foreach ( $this->getAllPerson() as $key => $value ) {
			echo "------insertEmp---------------" . PHP_EOL;
			Database::getInstance()->insert( "gangwan_emp", $value );
		}
	}


	private function getAllPerson() {
		$result=json_decode( Curl_request( self::$apiUrl
		                                   . "?type=getAllPerson&format=json" ),
			false );
		var_dump( $result->data['employee']['employeeList']);exit();
		return $result->data['employee']['employeeList']['employeeInfo'];
	}

	private function getAllOrg() {
		$result=json_decode( Curl_request( self::$apiUrl
		                                   . "?type=getAllOrg&format=json" ),
			false );
		return $result->data->OrganizationInterface->organInterfaceList->organInterfaceInfo;
	}


	private function getEmpNew() {
		return json_decode( Curl_request( self::$apiUrl
		                                  . "?type=getEmpNew&format=json" )['data'],
			true );
	}


	private function getLastUpdateTime() {
		$now       = microtime();
		$yesterday = $now - self::LAST_UPDATE_INTERVAL;

		return date( "yyyy/MM/dd HH:mm:ss", $yesterday );
	}


}